package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 负责与dubbo服务进行交互
 */
@Service
public class RecommendUserService {

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    /**
     * 调用dubbo服务提供者，进行今日佳人数据查询
     * @param userId
     * @return
     */
    public TodayBest queryTodayBest(Long userId) {
        //1.调用dubbo服务，查询今日佳人数据
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        //2.非空判断
        if(recommendUser == null){
            return null;
        }

        //3.构建一个TodayBest对象
        TodayBest todayBest = new TodayBest();
        //3.1 设置TodayBest的id
        todayBest.setId(recommendUser.getUserId());

        //3.2 设置TodayBest的缘分值
        double score = Math.floor(recommendUser.getScore());        //取整,98.2 -> 98
        todayBest.setFateValue(Double.valueOf(score).longValue());

        return todayBest;
    }

    /**
     * 调用dubbo服务提供者，进行推荐用户列表查询
     * @param id        接收推荐信息的用户id
     * @param page      当前页码
     * @param pagesize  每页条数
     * @return
     */
    public PageInfo<RecommendUser> queryRecommendUserList(Long id, Integer page, Integer pagesize) {
        return recommendUserApi.queryPageInfo(id, page, pagesize);
    }
    /**
     * 查询推荐好友的缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    public Double queryScore(Long userId, Long toUserId){
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if(ObjectUtil.isNotEmpty(score)){
            return score;
        }
        //默认值
        return 98d;
    }


    /**
     * 查询探花列表，查询时需要排除喜欢与不喜欢列表用户
     * @param id
     * @param count
     * @return
     */
    public List<RecommendUser> queryCardList(Long id, Integer count) {
        return recommendUserApi.queryCardList(id, count);
    }
}

